package com.exampleyyy.demoyyy.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.exampleyyy.demoyyy.JieKouService.ShoppingCartServiceJieKou;
import com.exampleyyy.demoyyy.ShiTiYYY.ShoppingCart;
import com.exampleyyy.demoyyy.common.BaseContext;
import com.exampleyyy.demoyyy.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartServiceJieKou shoppingCartServiceJieKou;
    @PostMapping("/add")
    public R<Object> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        // 获取当前登录对象的ID
        shoppingCart.setUserId(currentId);
        // 给购物车对象添加用户ID属性之为currentId

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if(dishId != null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询当前菜品或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart cartServiceOne = shoppingCartServiceJieKou.getOne(queryWrapper);

        if(cartServiceOne != null){
            //如果已经存在，就在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            //  update shopping_cart set number=4 where userid = 当前登录用户
            shoppingCartServiceJieKou.updateById(cartServiceOne);
        }else{
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            // shoppingCart.setCreateTime(LocalDateTime.now());

            // insert into shopping_cart (number，userid)  values (1,当前登录用户id)
            shoppingCartServiceJieKou.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(1,"添加购物车成功",cartServiceOne);
    }
    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<Object> list(){
        // select * from  shopping_cart where userid= 当前登录用户id
        log.info("查看购物车...");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartServiceJieKou.list(queryWrapper);
        return R.success(1,"购物车查询成功",list);
    }
    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<Object> clean(){
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartServiceJieKou.remove(queryWrapper);
        return R.success(1,"清空购物车成功",null);
    }
    @PostMapping("/sub")
    public R<Object> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("删除购物车商品:{}", shoppingCart);

        // 设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        if (dishId != null) {
            // 删除的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            // 删除的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 查询当前菜品或者套餐是否在购物车中
        // SQL: select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart cartServiceOne = shoppingCartServiceJieKou.getOne(queryWrapper);

        if (cartServiceOne != null) {
            // 如果已经存在，就在原来数量基础上减一
            Integer number = cartServiceOne.getNumber();
            if (number > 1) {
                cartServiceOne.setNumber(number - 1);
                // update shopping_cart set number = ? where userid = 当前登录用户
                shoppingCartServiceJieKou.updateById(cartServiceOne);
            } else {
                // 如果数量为1，直接删除该记录
                // delete from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
                shoppingCartServiceJieKou.remove(queryWrapper);
            }
            return R.success(1, "删除购物车商品成功", cartServiceOne);
        } else {
            // 购物车中不存在该商品
            return R.error(1, "购物车中无该商品", null);
        }
    }

}